<template>
	<div class="table-demo-container layout-padding">
		<div class="table-demo-padding layout-padding-view layout-padding-auto">
			<TableSearch :search="state.tableData.search" @search="onSearch" />
			<Table ref="tableRef" v-bind="state.tableData" class="table-demo" @delRow="onTableDelRow"
				@editRow="ResetLeavePerson" @pageChange="onTablePageChange" @sortHeader="onSortHeader" />
		</div>
	</div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, reactive, ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { usePersonEmployeeLeavePageApi, usePersonEmployeeLeaveResetApi, useLeavePersonEmployeeDeleteApi } from '/@/api/person/leave';

// 引入组件
const Table = defineAsyncComponent(() => import('/@/views/person/leave/table/index.vue'));
const TableSearch = defineAsyncComponent(() => import('/@/components/table/noAddSearch.vue'));

// 定义变量内容
const tableRef = ref<RefType>();
const state = reactive<TableDemoState>({
	tableData: {
		// 列表数据（必传）
		data: [],
		// 表头内容（必传，注意格式）
		header: [
			{ key: 'code', colWidth: '', title: '工号', type: 'text', isCheck: true, sortable: true },
			{ key: 'name', colWidth: '', title: '姓名', type: 'text', isCheck: true, sortable: true },
			{ key: 'orgName', colWidth: '', title: '部门', type: 'text', isCheck: true, sortable: true },
			{ key: 'roleName', colWidth: '', title: '角色', type: 'text', isCheck: true, sortable: true },
			{ key: 'reason', colWidth: '', title: '离职原因', type: 'text', isCheck: true, sortable: true },
			{ key: 'deletedAt', colWidth: '', title: '离职时间', type: 'text', isCheck: true, sortable: true },
		],
		// 配置项（必传）
		config: {
			total: 0, // 列表总数
			loading: true, // loading 加载
			isBorder: true, // 是否显示表格边框
			isSerialNo: false, // 是否显示表格序号
			isSelection: false, // 是否显示表格多选
			isOperate: true, // 是否显示表格操作栏
		},
		// 搜索表单，动态生成（传空数组时，将不显示搜索，注意格式）
		search: [
			{ label: '工号', prop: 'code', placeholder: '请输入工号', required: false, type: 'input', defaultValue: '' },
			{ label: '姓名', prop: 'name', placeholder: '请输入姓名', required: false, type: 'input', defaultValue: '' },
		],
		// 搜索参数（不用传，用于分页、搜索时传给后台的值，`getTableData` 中使用）
		param: {
			pageNum: 1,
			pageSize: 10,
		},
		// 打印标题
		printName: '打印示例',
	},
});

// 初始化列表数据
const getTableData = () => {
	state.tableData.config.loading = true;
	state.tableData.data = [];
	usePersonEmployeeLeavePageApi(state.tableData.param).then(({ content }) => {
		state.tableData.data = content.list || []
		state.tableData.param.pageSize = content.pageSize
		state.tableData.param.pageNum = content.pageNum
		state.tableData.config.total = content.total
	})
	// 数据总数（模拟，真实从接口取）
	state.tableData.config.loading = false;
};

// 搜索点击时表单回调
const onSearch = (data: EmptyObjectType) => {
	state.tableData.param = Object.assign({}, state.tableData.param, { ...data });
	getTableData()
};

// 彻底删除回调
const onTableDelRow = (row: EmptyObjectType) => {
	ElMessageBox.confirm("此操作将永久删除该员工信息, 是否继续?", '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		useLeavePersonEmployeeDeleteApi(row.code).then(() => {
			ElMessage.success(`删除成功！`);
			getTableData();
		})
	});
};

// 恢复离职员工回调
const ResetLeavePerson = (row: EmptyObjectType) => {
	ElMessageBox.confirm("是否恢复该员工信息？", '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		usePersonEmployeeLeaveResetApi({ code: row.code }).then(() => {
			ElMessage.success(`恢复成功！`);
			getTableData();
		})
	});
};

// 分页改变时回调
const onTablePageChange = (page: TableDemoPageType) => {
	state.tableData.param.pageNum = page.pageNum;
	state.tableData.param.pageSize = page.pageSize;
	getTableData();
};
// 拖动显示列排序回调
const onSortHeader = (data: TableHeaderType[]) => {
	state.tableData.header = data;
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.table-demo-container {
	.table-demo-padding {
		padding: 15px;

		.table-demo {
			flex: 1;
			overflow: hidden;
		}
	}
}
</style>
